import 'dart:convert';

import 'package:excel_utils/pages/daily/daily_data_entry/bean/third_bean.dart';
import 'package:excel_utils/repository/constant.dart';
import 'package:excel_utils/service/DioManager.dart';

abstract class IDailyEntryRepository {
  Future<ThirdBean?> saveThird(ThirdBean thirdBean);
  Future<List<ThirdBean>> saveAllThird(List<ThirdBean> thirdData);
  Future<List<ThirdBean>> findThirdByDateTime(String startDate, String endDate);
  Future<void> deleteThird(int id);
}

class DailyEntryRepository implements IDailyEntryRepository {
  @override
  Future<void> deleteThird(int id) async {
    final response = await DioManager().post(path: "${Constant.deleteThirdPartyUrl}");
    if (response["code"] == 0) {
    } else {}
  }

  @override
  Future<List<ThirdBean>> findThirdByDateTime(String startDate, String endDate) async {
    final response = await DioManager().get(path: "${Constant.findThirdPartyUrl}?create_time_start=$startDate&create_time_end=$endDate");
    List<ThirdBean> data = [];
    dynamic result = json.decode(response.toString());
    if (result["code"] == 0) {
      result['data'].forEach((v) {
        data.add(ThirdBean.fromJson(v));
      });
      return data;
    } else {
      return [];
    }
  }

  @override
  Future<ThirdBean?> saveThird(ThirdBean thirdBean) async {
    var vvv = thirdBean.toJson();
    final response = await DioManager().post(path: "${Constant.saveThirdPartySignUrl}", data: thirdBean.toJson());
    dynamic result = json.decode(response.toString());
    if (result["code"] == 0) {
      return ThirdBean.fromJson(result["data"]);
    } else {
      return null;
    }
  }

  @override
  Future<List<ThirdBean>> saveAllThird(List<ThirdBean> thirdData) async {
    final response = await DioManager().post(path: "${Constant.saveThirdPartyBatchUrl}", data: json.encode(thirdData));
    List<ThirdBean> data = [];
    dynamic result = json.decode(response.toString());
    if (result["code"] == 0) {
      result['data'].forEach((v) {
        data.add(ThirdBean.fromJson(v));
      });
      return data;
    } else {
      return [];
    }
  }
}
